package com.example.demo.aop;


import com.example.demo.pojo.Learn;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Created by dingkl on 2017/7/14.
 */
@Aspect
@Order(value = 2)
@Component
public class TestAop2 {

    private Logger logger = LoggerFactory.getLogger(TestAop2.class.getName());


    @Pointcut(value = "execution(* com.example.demo.service.LearnService.add(..))&& args(learn)",argNames = "learn")
    public void beforePointcut(Learn learn){
    }

    @Before(value = "beforePointcut(learn)")
    public void before(Learn learn){
        logger.info(learn.getTitle());
        logger.info("before1");
    }

    @AfterReturning(value="beforePointcut(learn)")
    public void after_running(Learn learn){
        learn.setTitle(learn.getTitle()+"1");
        logger.info("after_running1");
    }

    @AfterThrowing(value = "beforePointcut(learn)")
    public void after_throwing(Learn learn){
        learn.setTitle(learn.getTitle()+"1");
        logger.info("after_throwing1");
    }

    @After(value="beforePointcut(learn)")
    public void after(Learn learn){
        logger.info("after1");
    }

    //注意返回值
    @Around(value = "beforePointcut(learn)")
    public int round(ProceedingJoinPoint point,Learn learn){
        int  i = 0;
        try {
            //可以不执行proceed，用于直接返回数据
            logger.info("round1 start");
            i =(Integer)point.proceed();
            logger.info("round1 end");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return i;
    }
}
